package com.avantis.admin.controller;

import com.avantis.admin.config.shiro.CurrentUser;
import com.avantis.admin.service.CommonCodeService;
import com.avantis.common.base.BaseController;
import com.avantis.common.entity.AdminUser;
import com.avantis.common.entity.CommonCode;
import com.avantis.common.entity.Company;
import com.avantis.common.entity.query.CommonCodeQuery;
import com.avantis.common.msg.YResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: tracywang
 * @Date: 2019/2/15
 * @Descrption:
 */
@RestController
@RequestMapping("/common")
public class CommonCodeController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommonCodeController.class);

    @Autowired
    private CommonCodeService commonCodeService;

    @PostMapping("/insert")
    @ApiOperation(value = "枚举类接口", httpMethod = "POST", notes = "枚举code自己加")
        public YResult insert(@RequestBody @Validated CommonCode commonCode) {
        YResult yResult = restProcessor(() -> {
                AdminUser adminUser = CurrentUser.get();
                commonCode.setCreater(adminUser.getUsername());
                commonCode.setCreatedon(new Date());
                commonCode.setModifyon(new Date());
                commonCodeService.saveCommonCode(commonCode);
                return  YResult.ok();
        });
        return yResult;
    }

    @GetMapping("/list/{codeType}")
    @ApiOperation(value = "枚举列表（适用于某种枚举集合）", httpMethod = "GET", notes = "")
    @ApiImplicitParams(@ApiImplicitParam(name = "codeType", value = "供应商ID", dataType = "string"))
    public YResult detail(@PathVariable("codeType") String codeType) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("codeList", commonCodeService.findCommonCodesByCodeType(codeType));
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }

    @PostMapping("/list")
    @ApiOperation(value = "枚举列表", httpMethod = "POST", notes = "")
    public YResult list(@RequestBody CommonCodeQuery commonCodeQuery) {
        YResult yResult = restProcessor(() -> {
            try {
                Map<String,Object> resultMap = new HashMap<>();
                resultMap.put("count",commonCodeService.findCommonCodeCriteria(commonCodeQuery).getTotalElements());
                resultMap.put("pageCount",commonCodeService.findCommonCodeCriteria(commonCodeQuery).getTotalPages());
                resultMap.put("models",commonCodeService.findCommonCodeCriteria(commonCodeQuery).getContent());
                resultMap.put("pageSize",commonCodeService.findCommonCodeCriteria(commonCodeQuery).getSize());
                resultMap.put("pageIndex",commonCodeService.findCommonCodeCriteria(commonCodeQuery).getNumber()+1);
                return YResult.ok(resultMap);
            } catch (Exception e) {
                return YResult.error(e.getMessage());
            }
        });
        return yResult;
    }
}
